package com.bailiban.qa.mapper;

import com.bailiban.common.entity.PageResult;
import com.bailiban.qa.entity.Problem;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhulang
 * @Classname ProblemMapper
 * @Description ProblemMapper
 */
@Repository
public interface ProblemMapper extends BaseMapper<Problem> {
    /**
     * 最新的问题
     *
     * @param labelid
     * @param start
     * @param size
     * @return
     */
    @Select(value = "select * from tb_problem tp join tb_pl tpl ON tp.id=tpl.problemid where tpl.labelid=#{labelid} ORDER BY tp.createtime DESC LIMIT #{start},#{size}")
    List<Problem> findLatestProblems(@Param("labelid") String labelid, @Param("start") int start, @Param("size") int size);

    @Select(value = "SELECT COUNT(1) from tb_problem tp join tb_pl tpl ON tp.id=tpl.problemid where tpl.labelid=#{labelid}")
    long total(@Param("labelid") String labelid);

    /**
     * 热门回答的问题
     *
     * @param labelid
     * @param start
     * @param size
     * @return
     */
    @Select(value = "select * from tb_problem tp join tb_pl tpl ON tp.id=tpl.problemid where tpl.labelid=#{labelid} ORDER BY tp.reply DESC LIMIT  #{start},#{size}")
    List<Problem> findHotProblems(@Param("labelid") String labelid, @Param("start") int start, @Param("size") int size);

    /**
     * 等待回复的问题
     *
     * @param labelid
     * @param start
     * @param size
     * @return
     */
    @Select(value = "select * from tb_problem tp join tb_pl tpl ON tp.id=tpl.problemid where tpl.labelid=#{labelid} and tp.reply=0  ORDER BY tp.createtime DESC LIMIT #{start},#{size}")
    List<Problem> findWaitProblems(@Param("labelid") String labelid, @Param("start") int start, @Param("size") int size);

    @Select(value = "SELECT COUNT(1) from tb_problem tp join tb_pl tpl ON tp.id=tpl.problemid where tpl.labelid=#{labelid} and tp.reply=0")
    long waitTotal(@Param("labelid") String labelid);
}
